﻿(function ($) {
    $.extend($, {
        //html字符编码
        HTMLEncode: function (str) {
            var s = "";
            if (str.length == 0) return "";
            s = str.replace(/&/g, "&amp;");
            s = s.replace(/</g, "&lt;");
            s = s.replace(/>/g, "&gt;");
            s = s.replace(/ /g, "&nbsp;");
            s = s.replace(/\'/g, "&#39;");
            s = s.replace(/\"/g, "&quot;");
            return s;
        },
        //html字符解码
        HTMLDecode: function (str) {
            var s = "";
            if (str.length == 0) return "";
            s = str.replace(/&amp;/g, "&");
            s = s.replace(/&lt;/g, "<");
            s = s.replace(/&gt;/g, ">");
            s = s.replace(/&nbsp;/g, " ");
            s = s.replace(/&#39;/g, "\'");
            s = s.replace(/&quot;/g, "\"");
            return s;
        },
        getQueryString: function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        },
        arrunique: function (array) {
            var ret = [], done = {};
            try {
                for (var i = 0, length = array.length; i < length; i++) {
                    var tmp = array[i]; // jQuery源码：var id = jQuery.data(array[i]);
                    if (!done[tmp]) {
                        done[tmp] = true;
                        ret.push(tmp);
                    }
                }
            } catch (e) {
                ret = array;
            }
            return ret;
        },
        dateDiff: function (startDateStr, endDateStr) {
            var begintime_ms = Date.parse(new Date(startDateStr.replace(/-/g, "/"))); //begintime 为开始时间
            var endtime_ms = Date.parse(new Date(endDateStr.replace(/-/g, "/")));   // endtime 为结束时间
            var diffValue = endtime_ms - begintime_ms;  //时间差的毫秒数
            return Math.floor(diffValue / (24 * 3600 * 1000));
        }
    });
})(jQuery);